package com.jcl.recruit.user.logic;

import com.jcl.core.utils.EncodeMd;
import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName UserRegisterLogic

 * @Author yyliu
 * @Date 2018/9/14 13:16
 * @Version 1.0
 **/
@Component
public class UserRegisterLogic {

    @Autowired
    private UserLogic userLogic;

    /* *
     * @Author yyliu
     * @Description 注册企业用户
     * @Date 12:32 2018/9/21
     * @Param [user, deparmentId]
     * @return com.example.model.User
     **/
    public User registerAdminUser(User user,
                                  Integer departId) {

        try {
            user.setPassword(EncodeMd.EncodeByMd5(SystemConfig.getDefaultPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        user.setDepartId(departId);
        user.setHr(true);
        // 默认 注册有效
        user.setStatus(Constants.UserInfo.USER_STATUS_ACTIVE);
        // 默认管理员
        user.setRoles(Constants.UserInfo.createAdminAndHrRole());
        user.setCreateTime(new Date());
        userLogic.insertSelective(user);
        return user;
    }

    /* *
     * @Author yyliu
     * @Description 注册申请用户
     * @Date 13:41 2018/9/14
     * @Param [user]
     * @return com.example.model.User
     **/
    public User tryRegisterUser(User user, String corpCode, Integer departId) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        Map<String, Object> _keyMap = userLogic.selectMaxIdKey();
        Integer maxId = 0;
        if (null != _keyMap && null != _keyMap.get("maxId")) {
            maxId = Integer.parseInt(_keyMap.get("maxId").toString());
        }
        maxId++;
        user.setCorpCode(corpCode);
        user.setId(maxId);
        user.setDepartId(departId);
        user.setPassword(EncodeMd.EncodeByMd5(SystemConfig.getDefaultPassword()));
        user.setCreateTime(new Date());
        user.setStatus(Constants.UserInfo.USER_STATUS_ACTIVE);
        user.setRoles(Constants.UserInfo.createHrRole());
        userLogic.insertSelective(user);
        return user;
    }


}
